﻿using Microsoft.Data.SqlClient;
using SqlSugar;
using System.Data;
using System.Reflection;

namespace Finance.Api.Write.Extensions
{
    public static class ServiceCollectionExtension
    {
        /// <summary>
        /// 封装mapper和automapper和中介者media
        /// </summary>
        /// <param name="services"></param>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IServiceCollection AddServices(this IServiceCollection services, WebApplicationBuilder builder)
        {
            services.AddScoped<ISqlSugarClient>(s =>
            {
                //Scoped用SqlSugarClient 
                SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = SqlSugar.DbType.MySql,
                    ConnectionString = builder.Configuration.GetConnectionString("conn"),
                    IsAutoCloseConnection = true,
                });
                return sqlSugar;
            });
            services.AddMediatR(x => x.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
            services.AddAutoMapper(typeof(MappingProfile));


            return services;

        }
        ///// <summary>
        ///// 封装dapper
        ///// </summary>
        ///// <param name="services"></param>
        ///// <param name="builder"></param>
        ///// <returns></returns>
        //public static IServiceCollection DapperHelper(this IServiceCollection services, WebApplicationBuilder builder)
        //{
        //    // 注册IDbConnection，从SqlSugar获取连接
        //    services.AddScoped<IDbConnection>(s =>
        //    {
        //        var connection = new SqlConnection(builder.Configuration.GetConnectionString("conn"));
        //        connection.Open();
        //        return connection;
        //    });
        //    return services;
        //}
    }
}
